﻿#include "dataframe.h"

using namespace IGBTProtocol;

DataFrame::DataFrame()
    : BaseFrame(FrameHeader_Data, 0)
{
    _dataChannel = DataChannel_Undef;
    _dataSource = DataSource_Undef;

    _dataByteSum = 0;
    _data = NULL;
}


DataFrame::~DataFrame()
{
    if(_data != NULL)
        delete[] _data;
}

unsigned short DataFrame::channelSampleNum(unsigned char channelNum, unsigned char bytesPerSample) const
{
    if(_dataSize <= 6)
        return 0;

    return (_dataSize - 6) / (bytesPerSample * channelNum);
}

bool DataFrame::depack(QByteArray* buff)
{
    if(!BaseFrame::depack(buff))
        return false;

    if((unsigned char)(buff->at(9)) == DataChannel_HRate)
        _dataChannel = DataChannel_HRate;
    else if((unsigned char)(buff->at(9)) == DataChannel_LRate)
        _dataChannel = DataChannel_LRate;
    else return false;

    if((unsigned char)(buff->at(10)) == DataSource_RealTime)
        _dataSource = DataSource_RealTime;
    else if((unsigned char)(buff->at(10)) == DataSource_SD)
        _dataSource = DataSource_SD;
    else return false;

    _dataByteSum = 0;
    unsigned int temp = (unsigned char)(buff->at(11));
    _dataByteSum += temp;
    temp = (unsigned char)(buff->at(12));
    _dataByteSum += (temp << 8);
    temp = (unsigned char)(buff->at(13));
    _dataByteSum += (temp << 16);
    temp = (unsigned char)(buff->at(14));
    _dataByteSum += (temp << 24);

    if(_data != NULL)
        delete[] _data;

    _data = new unsigned char[_dataSize - 6];
    memcpy(_data, buff->data() + 15, _dataSize - 6);
    return true;
}
